/*
 * @Author: huangtianyang 916072572@qq.com
 * @Date: 2023-11-21 09:31:27
 * @LastEditors: huangtianyang
 * @LastEditTime: 2024-04-24 08:59:18
 * @FilePath: \cxl-driving-training\src\views\drivingSchoolManagement\coach\list\index.ts
 */
import { BaseData, BaseMethod } from '@/utils/basc-data'
import { tableConfig } from './config'
import { reactive } from 'vue'
import router from '@/router'
import { coachDisable, coachList, coachListInterface, importCoach } from '@/apis/coach'
import { fileUrl } from '@/apis/upload'
import { userStore } from '@/store/user.store'
import { ElMessage } from 'element-plus'
import { CreateAxiosDownload, CreateXMLHttpRequest } from '@/utils'

export default class CoachData extends BaseData implements BaseMethod {
    search = { schoolId: '', name: '', phone: '', teachAge: '', sex: '', status: '' }
    uStore = userStore()
    constructor() {
        super(tableConfig)
        const options: { label: string; value: string }[] = []
        this.uStore.queryDrivingSchoolSimple((res: Map<string, any>) => {
            res.forEach(item => {
                options.push({ label: item.shortName, value: item.id })
            })
            tableConfig.search[0].options = options
            console.log(options)
        })
        this.pageData.pagination.pageSize = 10
        this.getData()
    }
    getData = (arg?: any) => {
        const params: coachListInterface = {
            current: this.pageData.pagination.pageNo,
            size: this.pageData.pagination.pageSize
        }

        if (this.search.schoolId !== '' && this.search.schoolId !== '-1') {
            params.schoolId = this.search.schoolId
        }
        if (this.search.name) {
            params.name = this.search.name
        }
        if (this.search.phone) {
            params.phone = this.search.phone
        }
        if (this.search.teachAge) {
            params.teachAge = Number(this.search.teachAge)
        }
        if (this.search.sex !== '' && this.search.sex !== '-1') {
            params.sex = Number(this.search.sex)
        }
        if (this.search.status !== '' && this.search.status !== '-1') {
            params.disable = Number(this.search.status)
        }

        coachList(params).then(res => {
            if (Number(res.data.code) === 200) {
                this.pageData.table.data.length = 0
                this.pageData.pagination.total = res.data.result.total
                const arr: any[] = res.data.result.records
                console.log(this.uStore.drivingSchoolSimple)
                arr.forEach((element: any) => {
                    element.imageUrl = element.headPicUrl
                    element.status = Number(element.disable) === 0 ? true : false
                    element.school = this.uStore.drivingSchoolSimple.get(element.schoolId)?.fullName
                    this.pageData.table.data.push(element)
                })
            }
        })
    }
    onSearch = (arg?: any) => {
        console.log(arg)
        this.search = arg
        this.getData()
    }
    onReset = (arg?: any) => {
        this.search = { schoolId: '', name: '', phone: '', teachAge: '', sex: '', status: '' }
        this.getData()
    }
    onPaginationChange = (arg?: any) => {
        this.pageData.pagination.pageNo = arg
        this.getData()
    }
    onPaginationSize = (arg?: any) => {
        this.pageData.pagination.pageNo = 1
        this.pageData.pagination.pageSize = arg
        this.getData()
    }
    onOperation = (arg?: any) => {
        const { type, data } = arg
        console.log(type, data)
        if (type === 'create') {
            router.push({ path: '/coach/operation', query: { type: 'create' } })
        } else if (type === 'edit' || type === 'look') {
            router.push({ path: '/coach/operation', query: { type, id: data.id } })
        } else if (type === 'import') {
            importCoach(data[0]).then(res => {
                if (Number(res.data.code) === 200) {
                    ElMessage.success('上传成功')
                    this.getData()
                }
            })
        } else if (type === 'download') {
            //科二训练场：trainSiteTwo, 科三训练场：trainSiteThree, 驾校教练：schoolCoach;
            const url = import.meta.env.VITE_HOST + '/api/system/template/download/schoolCoach'

            CreateAxiosDownload(url, '教练模板')
        } else if (type === 'switch') {
            coachDisable(data.status ? 0 : 1, data.id).then(res => {
                if (Number(res.data.code) === 200) {
                    ElMessage.success('修改成功')
                    // this.getData()
                }
            })
        }
    }
}
